<?php
$include_path = "../";
include_once("../util/db_manager.php");
require_once("admin_data_base_functions.php");
$error = false;
$msg = "";
$total = 0;
class Plant {
	var $plant_id;
	var $name;
	var $availability;
	
	public static function getPlantList($result) {
		$plants = array();
		while ($row = mysql_fetch_array($result)) {
			$plant = new Plant();
			$plant->plant_id = $row["plant_id"];
			$plant->name = $row["name"];
			$plant->availability = $row["availability"];
			
			$plants[] = $plant;
		}
		return $plants;
	}
	
	public static function updatePlant($plant_id, $name, $availability) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "UPDATE plants SET availability=" . $availability . ", name='" . $name . "' WHERE plant_id=" . $plant_id;
			$dbManager->processQuery($query,$conn);
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
			$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
			$response = $response . getResponseEnd();
			return $response;
	}
	
	public static function addPlant($name, $availability) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "INSERT INTO plants(name, availability) VALUES('" . $name . "'," . $availability . ")";
			$dbManager->processQuery($query,$conn);
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
		$response = $response . getResponseEnd();
		return $response;
	}
	
	public static function getPlant($plant_id) {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "SELECT plant_id, name, availability FROM plants WHERE plant_id=" . $plant_id;
			$result = $dbManager->processQuery($query,$conn);
			$plants = Plant::getPlantList($result);
			$dbManager->releaseConnection($conn);
			$GLOBALS['total'] = 1;
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		return Plant::getPlantResponse($plants);
	}
	
	public static function getAllPlants() {
		try {
			//start db transaction
			$dbManager = DBManager::getInstance($GLOBALS['config_file']);
			$conn = $dbManager->fetchConnection();
			
			$query = "SELECT plant_id, name, availability FROM plants ORDER BY name";
			$result = $dbManager->processQuery($query,$conn);
			$plants = Plant::getPlantList($result);
			$query = "SELECT count(1) 'total' FROM plants";
			$result = $dbManager->processQuery($query,$conn);
			while ($row = mysql_fetch_array($result)) {
				$GLOBALS['total'] = $row["total"];
			}
			$dbManager->releaseConnection($conn);
			$GLOBALS['msg'] = "completed";
		} catch (Exception $e) {
			$GLOBALS['error'] = true;
			$GLOBALS['msg'] = $e->getMessage();
		} 
		return Plant::getPlantResponse($plants);
	}

	public static function getPlantResponse($plants) {
		$response = getResponseStart($GLOBALS['error'], $GLOBALS['msg'], $GLOBALS['total']);
		$response = $response . "<plants>";
		if ($plants != null) {
			for ($cntr = 0; $cntr < count($plants); $cntr++) {
				$plant = $plants[$cntr];
				$response = $response .  "<plant><id>";
				$response = $response .  $plant->plant_id;
				$response = $response .  "</id><name>";
				$response = $response .  "<![CDATA[" . $plant->name . "]]>";
				$response = $response .  "</name><availability>";
				$response = $response .  $plant->availability;
				$response = $response .  "</availability></plant>";
			}
		}
		$response = $response . "</plants>";
		$response = $response . getResponseEnd();
		return $response;
	}
}
?>
